/** ***********************************************************************************
* @file         files.c
* @brief        文件接口
* @details      
* @author       
* @date         2021/03
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/
#define OSI_LOG_TAG OSI_MAKE_LOG_TAG('F', 'I', 'L', 'E')
#include "os_app.h"
#include "log_task.h"
#include "algorithm.h"
#include "files.h"

GaozSemaphoreHandle_t   file_termpara_mutex = 0; //文件互斥
/** ****************************************************************************
* @remarks       void file_termpara_init( void )
* @brief         资源初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void file_termpara_init( void )
{
    file_termpara_mutex = gaoz_mutex_create();
    if( file_termpara_mutex == 0 )
    {
        while(1); /*fail*/
    }
}

/** ****************************************************************************
* @remarks       int file_termpara_read(const INT8 *filename,INT8 *pda)
* @brief         读取文件数据
* @param[in]     filename 路径
* @param[out]    * da 数据的指针
* @return        -1:operation fail,-2:date error 0:succ
* @attention     
*******************************************************************************/
int file_termpara_read(const INT8 *filename,INT8 *pda)
{
    char *buf = NULL;

    if( file_termpara_mutex != 0 )
    {
        gaoz_mutex_lock(file_termpara_mutex);
    }
    else
    {
        return -1;
    }
    
    int filesize = fibo_file_getSize(filename);
    if (filesize < 0)
    {
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }

    int fd = fibo_file_open(filename, FS_O_RDONLY);
    if (fd < 0)
    {
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }

    buf = gaoz_fibo_malloc(filesize);
    if (buf == NULL)
    {
        fibo_file_close(fd);
        gaoz_fibo_free(buf);
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }

    int ret = fibo_file_read(fd, (UINT8 *)buf, filesize);
    if (ret < 0)
    {
        fibo_file_close(fd);
        gaoz_fibo_free(buf);
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }

    if(memcmp(filename,TERMID_FILE,strlen(TERMID_FILE)) == 0)
    {
        if(memcmp(&buf[4],"EDTT",4) != 0)
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,4);
        log_debug_printf(INFOR_MODE,"FILE","termid:%#x,%#x,%#x,%#x\r\n",buf[0], buf[1], buf[2], buf[3] );
    }
    else if(memcmp(filename,(INT8 *)RELAYPARA,strlen(RELAYPARA)) == 0)
    {
        if (get_chk_xor((uint8_t *)buf,filesize) != 0)
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,filesize); 
    }
    else if(memcmp(filename,RSAPARA,strlen(RSAPARA)) == 0)
    {
        if ((get_chk_xor((uint8_t *)buf,filesize) != 0)||(filesize > 256))
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,filesize); 
    }
    else if(memcmp(filename,LOGSAVEPARA,strlen(LOGSAVEPARA)) == 0)
    {
        if (get_chk_xor((uint8_t *)buf,filesize) != 0)
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,filesize);          
    }
    else if(memcmp(filename,TERMOTA,strlen(TERMOTA)) == 0)
    {
        if (get_chk_xor((uint8_t *)buf,filesize) != 0)
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,filesize); 
    }
    else
    {
        if (get_chk_xor((uint8_t *)buf,filesize) != 0)
        {
            fibo_file_close(fd);
            gaoz_fibo_free(buf);
            gaoz_mutex_unlock(file_termpara_mutex);
            return -2;        
        }
        memcpy(pda,buf,filesize); 
    }

    fibo_file_close(fd);
    gaoz_fibo_free(buf);
    gaoz_mutex_unlock(file_termpara_mutex);

    return 0;
}

/** ****************************************************************************
* @remarks       int file_termpara_write(const INT8 *filename,INT8 *pda,int len)
* @brief         读取文件数据
* @param[in]     filename 路径
* @param[out]    无
* @return        -1:operation fail, 非0：写入成功长度
* @attention     
*******************************************************************************/
int file_termpara_write(const INT8 *filename,INT8 *pda,int len)
{

    if( file_termpara_mutex != 0 )
    {
        gaoz_mutex_lock(file_termpara_mutex);
    }
    else
    {
        return -1;
    }

    int fd = fibo_file_open(filename, FS_O_RDWR | FS_O_CREAT | FS_O_TRUNC);
    if (fd < 0)
    {
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }

    char *content = (char *)gaoz_fibo_malloc(1024*2);
	if (content == NULL)
	{
		fibo_file_close(fd);
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
	}
    if((len != 0) && len < 1024*2)
    {
        memcpy(content,pda,len);
    }
    if (content != NULL)
    {
        //INT32 file_offset = fibo_file_seek(fd,8,FS_SEEK_CUR); 
        log_debug_printf(INFOR_MODE,"FILE","content = %s,%d\r\n", content,len);
        int ret = fibo_file_write(fd, (UINT8 *)content, len);
        fibo_file_close(fd);
        gaoz_fibo_free(content);
        gaoz_mutex_unlock(file_termpara_mutex);
        return ret;
    }
    else
    {
        gaoz_fibo_free(content);
        gaoz_mutex_unlock(file_termpara_mutex);
        return -1;
    }
}



